package classes{
	import beniFlex3Lib.manager.EvtManager;
	
	import classes.vo.CommandData;
	import classes.vo.EventData;
	import classes.vo.ServiceData;
	import classes.vo.VOData;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.collections.ArrayCollection;
	import mx.utils.ObjectUtil;
	
	public class ExportFile{
		private static var _modelLocator:ModelLocator = ModelLocator.item;
		private static var _file:File = new File();
		private static var _fs:FileStream = new FileStream();
		
		private static var _saveFileContent:String;
		private static var _templateXML:XML = 
			<info>
				<base>
					<path></path>
					<appName></appName>
					<packageName></packageName>
				</base>
				
				<valueObject>
				</valueObject>
				
				<command>
				</command>
				
				<service>
				</service>
				
				<event>
				</event>
			</info>;
			
		
		public static function export():void{
			var xml:XML = ObjectUtil.copy(_templateXML) as XML;
			var voAry:Array = _modelLocator.voColl.source;
			var commandAry:Array = _modelLocator.commandColl.source;
			var eventAry:Array = _modelLocator.eventColl.source;
			var serviceAry:Array = _modelLocator.serviceColl.source;
			// test start
//			var voAry:Array = [new VOData('MediaInfo'), new VOData('UserInfo')];
//			var commandAry:Array = [new CommandData('GoHome', true, false), new CommandData('GoDeliver', false, true)];
//			var eventAry:Array = [new EventData('SDIS_DWIS', 'GoHome'), new EventData('DSIEW_DSI', 'GoDeliver')];
//			var serviceAry:Array = [new ServiceData('SLogin'), new ServiceData('SLogout')];
			// test end
			var i:int;
			var voData:VOData, eventData:EventData, serviceData:ServiceData, commandData:CommandData;
			var tempXML:XML;
			
			// do for base
			xml.base.path = _modelLocator.path;
			xml.base.appName = _modelLocator.appName;
			xml.base.packageName = _modelLocator.packageName;
			
			// do for value object
			for (i=0; i<voAry.length; i++){
				voData = voAry[i] as VOData;
				tempXML = 
					<item>
						<name>{voData.name}</name>
					</item>;
				xml.valueObject.appendChild(tempXML);
			}
			
			// do for command
			for (i=0; i<commandAry.length; i++){
				commandData = commandAry[i];
				tempXML = 
					<item>
						<name>{commandData.name}</name>
						<view>{commandData.isViewLocator}</view>
						<rpc>{commandData.isIRPCResponder}</rpc>
					</item>;
				xml.command.appendChild(tempXML);
			}
			
			// do for event
			for (i=0; i<eventAry.length; i++){
				eventData = eventAry[i];
				tempXML = 
					<item>
						<name>{eventData.name}</name>
						<cmd>{eventData.command}</cmd>
					</item>;
				xml.event.appendChild(tempXML);
			}
			
			// do for service
			for (i=0; i<serviceAry.length; i++){
				serviceData = serviceAry[i];
				tempXML = 
					<item>
						<name>{serviceData.name}</name>
					</item>;
				xml.service.appendChild(tempXML);
			}
			_saveFileContent = xml.toXMLString();
			_file.browseForSave('Select export file(xml)');
			_file.addEventListener(Event.SELECT, onSelectSave);
		}
		public static function importConfig():void{
			_file.browseForOpen('Select import file(xml)');
			_file.addEventListener(Event.SELECT, onSelectImport);
		}
		private static function doImport(xml:XML):void{
			var node:XMLList;
			var item:XML;
						
			// do for base
			_modelLocator.path = xml.base.path;
			_modelLocator.appName = xml.base.appName;
			_modelLocator.packageName = xml.base.packageName;
			
			
			_modelLocator.eventColl = new ArrayCollection();
			_modelLocator.serviceColl = new ArrayCollection(); 
			_modelLocator.commandColl = new ArrayCollection();
			_modelLocator.voColl = new ArrayCollection();
			
			// do for event
			node = xml.event;
			for each (item in node.children()){
				_modelLocator.eventColl.addItem(new EventData(String(item.name), String(item.cmd)));
			}
			
			// do for service
			node = xml.service;
			for each (item in node.children()){
				_modelLocator.serviceColl.addItem(new ServiceData(String(item.name)));
			}
			
			// do for command
			node = xml.command;
			for each (item in node.children()){
				_modelLocator.commandColl.addItem(new CommandData(String(item.name), String(item.view)=='true', String(item.rpc)=='true'));
			}
			
			// do for value object
			node = xml.valueObject;
			for each (item in node.children()){
				_modelLocator.voColl.addItem(new VOData(String(item.name)));
			}
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		private static function onSelectSave(evt:Event):void{
			EvtManager.removeListener(evt, onSelectSave);
			_fs.open(_file, FileMode.WRITE);
			_fs.writeUTFBytes(_saveFileContent);
			_fs.close();
		}
		private static function onSelectImport(evt:Event):void{
			var content:XML;
			EvtManager.removeListener(evt, onSelectImport);
			_fs.open(_file, FileMode.READ);
			content = new XML(_fs.readUTFBytes(_fs.bytesAvailable));
			_fs.close();
			doImport(content);
		}
		
		
		
		
		
	}
}